var data_All=JSON.stringify({"county":"","top":"0"})
var data_rightTop,data_Click,data_Proclick,data_leftBottom,leftCenterParam,data_leftTop;
var map2data='',map2header='',optionSelect='',countyName='',countySelect='';
var data_moneyAll=0,data_peopleAll=0,data_avl=0,Num=0;
var data_titlename="全市";
var data_proleftCenter,data_proleftBottom,data_proleftTop;
var data_countyData=['项目数','总人数','人均数'], data_countyArray=[];
var data_projectData=['地区数','总人数','人均数'],data_projectArray=[];
var data_unit=['个','人','元'];
var data_proCounty_righttop;
var data_zone_select,data_county_select,data_countyAll,data_county_choose;
var countydata_first="";
var setMoney,setNumpeo,setNumpro,settime;
var sortState=2,sortNum;
var totalTownship,data_rightTopLength;
//开始数据
function refresh(){
    $('.loading').show();
    $.ajax({
        type: "POST",
        url: baseurl+"county/allListProject",
        // data:county,
        dataType: "json",
        contentType:'application/json',
        success: function (data) {
               console.log(data); 
            data_rightTop='';
            data_rightTop = data;
            // if(data_rightTop.length>=64){
            //     data_rightTopLength=64
            // }else{
                data_rightTopLength=data_rightTop.length;
            // }
            optionSelect='',map2data='';
            countySelect='';
            data_moneyAll=0;
            data_countyAll=[];
            // console.log(xAxis_leftTop);
            if(countydata_first == ""){
                // console.log('内容为空')
                countydata_first=xAxis_leftTop;
            }
            $('.map2-row').next().empty();
            optionSelect+='<option value="全市">全项目('+data_rightTopLength+'类)</option>';
            countySelect+='<option value="全福州">全福州</option>';
            $.each(data_rightTop,function(i,n){
                data_moneyAll +=parseInt(n.total_capital);
                var a=i+1;
                // console.log(n.project_id);
                // if(n.project_id!='金秋助学金'){
                    optionSelect+='<option value='+n.project_id+'>'+a+'.'+n.project_id+'</option>'
                // }
            })
            data_countyAll.push('全福州');
            $.each(countydata_first,function(i,n){
                countySelect+='<option value='+n+'>'+n+'</option>';
                data_countyAll.push(n);
            })
            // console.log(countySelect);
            // console.log(data_countyAll);
            data_moneyAll=String(data_moneyAll).replace(/(\d)(?=(\d{3})+$)/g, "$1,");
            $('.allmoney').html('<span class="countyName">'+data_titlename+'</span>发放资金总额：<span class="moneyNum">'+data_moneyAll+'<span>元</span></span>');
            for(var a=0;a<21;a++){
                setMoney='',setNumpeo='',setNumpro='';
                setNumpeo =data_rightTop[a].total_person;
                setNumpro=data_rightTop[a].total_project;
                setMoney =parseInt((data_rightTop[a].total_capital)/10000);
                // console.log(setNumpeo);
                // console.log(setMoney);
                            map2data+='<div class="map2-colum">'+
                '<p class="map2-text" title='+data_rightTop[a].project_id+'（县市区）>'+data_rightTop[a].project_id+'</p>'+
                '<p class="map2-num" data-index0='+setMoney+' data-index1='+setNumpeo+' data-index2='+setNumpro+'><span>'+parseInt((data_rightTop[a].total_capital)/10000)+'</span><span class="setpro">万元</span></p>'+
                '</div>';
            }
            map2data='<div class="dataPosition">'+map2data+'</div>'
            // console.log(map2data);
            // console.log(data_moneyAll);
            $('.map1-row').children('select').empty();
            $('.map2-row').after(map2data);
            $('#select_proselect').empty();
            $('#select_county').empty();
            // $('.map1-row').children('.proselect').append(optionSelect);
            $('#select_proselect').append(optionSelect);
            $('#select_county').append(countySelect);
            // $('.map1-row').children('.allcounty').append(countySelect);
            data_zone_select=optionSelect;
            data_county_select=countySelect;
            $('.dataPosition').stop().animate({left:"0%"});
            $('.loading').hide();
            settime=setInterval(setInt,5000);
        }
    });
}
refresh();
//地区点击方法
function dataClick(newCounty) {
    $.ajax({
        type: "POST",
        url:baseurl+"county/allListCounty",
        data:newCounty,
        dataType: "json",
        contentType:'application/json',
        success: function (data) {
//             console.log(data);
            data_rightTop='';
            $('.map2-row').siblings().remove();  
            $('.loading').show();
            map2data='',data_moneyAll=0;
            data_peopleAll=0;
            data_countyArray=[];
            data_rightTop = data;
            data_county_choose=new Array();
            $.each(data_rightTop,function(i,n){
                data_moneyAll +=parseInt(n.total_capital);
                data_peopleAll +=parseInt(n.total_person);
            })
            data_avl=parseInt((data_moneyAll/data_peopleAll));
            data_countyArray.push(data_rightTop.length);
            data_countyArray.push(data_peopleAll);
            data_countyArray.push(data_avl);
            data_moneyAll=String(data_moneyAll).replace(/(\d)(?=(\d{3})+$)/g, "$1,");
            $('.allmoney').html('<span class="countyName county">'+data[0].county+'</span>发放资金总额：<span class="moneyNum">'+data_moneyAll+'<span>元</span></span>');
            for(var a=0;a<data_countyData.length;a++){
                if(a==sortState-1){
                    sortNum=(Number(-sortState));
                }else{
                    sortNum=a+1;
                }
            map2data+='<div class="map2-colum">'+
                '<p class="map2-text" onclick="sortCountyBtn('+sortNum+')">'+data_countyData[a]+'</p>'+
                '<p class="map2-num">'+data_countyArray[a]+'<span class="setpro">'+data_unit[a]+'</span></p>'+
                '</div>';
            }
            data_rightTop.length > 5 ? Num = 6 : Num = data_rightTop.length ;
            // console.log(Num);
            for(var b=0;b<Num;b++){
                if(data_rightTop[b].total_township==null || data_rightTop[b].total_township==''){
                    totalTownship=1
                }else{
                    totalTownship=data_rightTop[b].total_township
                }
                map2data+='<div class="map2-colum">'+
                '<p class="map2-text" title='+data_rightTop[b].project_id+'（乡镇街道数）>'+data_rightTop[b].project_id+'</p>'+
                '<p class="map2-num" ><span>'+totalTownship+'<span class="setpro">个</span></span></p>'+
                '</div>';
                map2data+='<div class="map2-colum">'+
                '<p class="map2-text" title='+data_rightTop[b].project_id+'发放人数>发放人数</p>'+
                '<p class="map2-num">'+data_rightTop[b].total_person+'<span class="setpro">人</span></p>'+
                '</div>';
                map2data+='<div class="map2-colum">'+
                '<p class="map2-text" title='+data_rightTop[b].project_id+'发放金额（万元）>发放金额</p>'+
                '<p class="map2-num">'+(parseInt(data_rightTop[b].total_capital/10000))+'<span class="setpro">万元</span></p>'+
                '</div>';
            }
            map2data='<div class="dataPosition" style="left:100%">'+map2data+'</div>'
            // $('.map1-row').children('.proselect').empty();
            $('#select_proselect').empty();
            // $('.map1-row').children('.proselect').append(data_zone_select);
            $('#select_proselect').append(data_zone_select);
            $('.map2-row').after(map2data);
            $('.dataPosition').stop().animate({left:"0%"});
            $('.loading').hide();
        }
    });
  }
  //地区点击
//   $('.btnClick').click(function(){
//     data_titlename=$(this).text();
//     data_Click=JSON.stringify({"county":data_titlename});
//     data_leftBottom=JSON.stringify({"county":data_titlename,'top':'5'});
//     LeftBottomZone(data_leftBottom)//左三
//     $('.map2-row').siblings().remove();  
//     $('.map2-row').children('select').empty();
//     dataClick(data_Click);//右边
//     leftCenterParam=JSON.stringify({'county':data_titlename,'top':"5"});
//     dataYear(leftCenterParam);//左二
//     data_leftTop=JSON.stringify({'county':data_titlename});
//     dataZone(data_leftTop);//左一
//   })
  //项目点击方法
  function dataProclick(project){
    $.ajax({
        type: "POST",
        url: baseurl+"county/pitchListProject",
        data:project,
        dataType: "json",
        contentType:'application/json',
        success: function (data) {
               console.log(data);
            $('.map2-row').siblings().remove();  
            $('.loading').show();
            data_rightTop='';
            map2data='',data_moneyAll=0;
            optionSelect='';
            data_projectArray=[];
            data_rightTop = data;
            data_peopleAll=0;
            data_avl=0;
            $.each(data_rightTop,function(i,n){
                data_moneyAll +=parseInt(n.total_capital);
                data_peopleAll +=parseInt(n.total_person);
            })
            data_avl=parseInt((data_moneyAll/data_peopleAll));
            data_projectArray.push(data_rightTop.length);
            data_projectArray.push(data_peopleAll);
            data_projectArray.push(data_avl);
            data_moneyAll=String(data_moneyAll).replace(/(\d)(?=(\d{3})+$)/g, "$1,");
            $('.allmoney').html('<span class="countyName">'+data_titlename+'</span>资金总额：<span class="moneyNum">'+data_moneyAll+'<span>元</span></span>');
            for(var a=0;a<data_projectData.length;a++){
                if(a==sortState-1){
                    sortNum=(Number(-sortState));
                }else{
                    sortNum=a+1;
                }
                map2data+='<div class="map2-colum">'+
                    '<p class="map2-text" onclick="sortProBtn('+sortNum+')">'+data_projectData[a]+'</p>'+
                    '<p class="map2-num">'+data_projectArray[a]+'<span class="setpro">'+data_unit[a]+'</span></p>'+
                    '</div>';
            }
            data_rightTop.length > 5 ? Num = 6 : Num = data_rightTop.length ;

            for(var c=0;c<Num;c++){
                if(data_rightTop[c].total_township==null ||data_rightTop[c].total_township==''){
                    totalTownship=1
                }else{
                    totalTownship=data_rightTop[c].total_township
                }
                map2data+='<div class="map2-colum">'+
                '<p class="map2-text" title='+data_rightTop[c].county+'（乡镇街道数）>'+data_rightTop[c].county+'</p>'+
                '<p class="map2-num "><span>'+totalTownship+'<span class="setpro">个</span></span></p>'+
                '</div>';
                map2data+='<div class="map2-colum">'+
                '<p class="map2-text" title='+data_rightTop[c].county+'发放人数>发放人数</p>'+
                '<p class="map2-num" >'+data_rightTop[c].total_person+'<span class="setpro">人</span></p>'+
                '</div>';
                map2data+='<div class="map2-colum">'+
                '<p class="map2-text" title='+data_rightTop[c].county+'发放金额（万元）>发放金额</p>'+
                '<p class="map2-num">'+(parseInt(data_rightTop[c].total_capital/10000))+'<span class="setpro">万元</span></p>'+
                '</div>';
            }
            map2data='<div class="dataPosition" style="left:100%">'+map2data+'</div>';
            $('.map1-row').children('.allcounty').empty();
            $('#select_county').empty();
            // $('.map1-row').children('.allcounty').append(data_county_select);
            $('#select_county').append(countySelect);
            $('.map2-row').after(map2data);
            $('.dataPosition').stop().animate({left:"0%"});
            $('.loading').hide();
        }
    });
  }
  var countyif;
  $('.proselect').change(function(){
    sortState=Math.abs(2);
    //   console.log($(this).val());
    // countyName=$('.countyName').text();
    // console.log(countyName);
    clearInterval(settime);
    data_titlename=$(this).val();
    // console.log(data_titlename);
    $('.dataPosition').stop().animate({left:"150%"},"fast");
    $('.loading').show();
    if(data_titlename =='全市'){
        data_All=JSON.stringify({"county":"","top":"0"})
        refresh(data_All);
        startLeftTop();
        leftCenterDate=JSON.stringify({
            'year':new Date().getFullYear()
        })
        startLeftCenter(leftCenterParam);
        startLeftBottom();
        startMapdata();
        return;
    }
    myChart1.showLoading({
        text : "",
        maskColor:"rgba(53, 192, 255, 0)",
        });
        myChart2.showLoading({
        text : "",
        maskColor:"rgba(53, 192, 255, 0)",
        });
        myChart3.showLoading({
        text : "",
        maskColor:"rgba(53, 192, 255, 0)",
        });
    // if($('.countyName').hasClass('county') || countyif==true){
    //     console.log('地区里面的项目');
    //     countyif=true;
    //     data_proCounty_righttop=JSON.stringify({"project":data_titlename,'county':countyName});
    //     dataProCounty(data_proCounty_righttop);
    //     return;
    // }
    data_Proclick=JSON.stringify({"project":data_titlename});
    data_proleftBottom=JSON.stringify({"project":data_titlename,'top':'7'});
    data_proleftCenter=JSON.stringify({"project":data_titlename,'year':new Date().getFullYear()});
    data_proleftTop=JSON.stringify({"project":data_titlename});
    // $('.map2-row').children('select').empty();
    dataProclick(data_Proclick);//右边
    leftbottomPro(data_proleftBottom);//左三
    dataProyear(data_proleftCenter);//左二
    dataPro(data_proleftTop);//左一
    dataMap(data_Proclick);//中间地图
  })
$('.allcounty').change(function () {
    sortState=Math.abs(2);
    clearInterval(settime);
    console.log($(this).val());
    data_titlename = $(this).val();  
    myChart1.showLoading({
    text : "",
    maskColor:"rgba(53, 192, 255, 0)",
    });
    myChart2.showLoading({
    text : "",
    maskColor:"rgba(53, 192, 255, 0)",
    });
    myChart3.showLoading({
    text : "",
    maskColor:"rgba(53, 192, 255, 0)",
    });
    if(data_titlename=='全福州'){
        //右边
        data_titlename="全市";
        $('.dataPosition').remove();
        // $('.map1-row').children('select').empty();
        //左1
        startLeftTop();
        //左2
        leftCenterDate=JSON.stringify({
            'year':new Date().getFullYear()
        })
        startLeftCenter(leftCenterDate);
        //左3
        startLeftBottom();
        refresh();
        return;
    }
    data_Click=JSON.stringify({"county":data_titlename});
    data_leftBottom=JSON.stringify({"county":data_titlename,'top':'7'});
    LeftBottomZone(data_leftBottom)//左三
    $('.dataPosition').stop().animate({left:"100%"},"fast");
    // $('.map2-row').children('select').empty();
    dataClick(data_Click);//右边
    leftCenterParam=JSON.stringify({'county':data_titlename, 'year':new Date().getFullYear()});
    dataYear(leftCenterParam);//左二
    data_leftTop=JSON.stringify({'county':data_titlename});
    dataZone(data_leftTop);//左一
})
// var a=1,b,c=['万元','人','个'];
var a=1,c=['万元','人'];
function setInt(){
    for(var b=0;b<$('.map2-num').length;b++){
        $('.map2-num').eq(b).html('<span>'+$('.map2-num').eq(b).attr('data-index'+a+'')+'</span>'+'<span class="setpro">'+c[a]+'</span>');
    }
    a++;
    if(a>=2){
        a=0;
    }
}
// var settime=setInterval(setInt,3000)
// 排序(地区)
function sortCountyBtn(a){
    $('.loading').show();
    sortState=a;
    newCounty=JSON.stringify({"county":$('.countyName').text(),'type':''+a+''});
    dataClick(newCounty);
}
//排序(项目)
function sortProBtn(a){
    $('.loading').show();
    sortState=a;
    data_Proclick=JSON.stringify({"project":$('.countyName').text(),'type':''+a+''});
    dataProclick(data_Proclick);
}